package com.itguigu.auth.controller;

import com.itguigu.auth.service.SysMenuService;
import com.itguigu.common.result.Result;
import com.itguigu.model.system.SysMenu;
import com.itguigu.vo.system.AssginMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Api(tags = "菜单管理接口")
@RestController
@Slf4j
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("查询获取菜单")
    @GetMapping("findNodes")
    public Result findNodes(){
        List<SysMenu> list = sysMenuService.findNodes();
        return Result.success(list);
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增菜单")
    public Result save(@RequestBody SysMenu sysMenu){
        log.info("新增菜单的数据 sysMenu : {} " , sysMenu);
        boolean save = sysMenuService.save(sysMenu);
        return Result.success();
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除菜单")
    public Result remove(@PathVariable Long id){
        log.info("要删除的菜单的id : {}" , id);
        boolean b = sysMenuService.removeMenuById(id);
        return Result.success();
    }

    @ApiOperation("修改菜单数据")
    @PutMapping("update")
    public Result updateById(@RequestBody SysMenu sysMenu){
        log.info("要修改的菜单数据为 sysMenu : {}" , sysMenu);
        boolean b = sysMenuService.updateById(sysMenu);
        return Result.success();
    }


    @ApiOperation(value = "根据角色id查询菜单")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId){
        log.info("根据角色id 查询其对应的菜单 : {}" , roleId);
        List<SysMenu> menuMap = sysMenuService.findMenuByRoleId(roleId);
        return Result.success(menuMap);
    }

    @ApiOperation(value = "根据角色分配菜单")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo){
        log.info("根据角色分配菜单 assginMenuVo : {}" , assginMenuVo);
        sysMenuService.doAssign(assginMenuVo);
        return Result.success();
    }

}
